import { ref, watch,onMounted} from 'vue'
import { defineStore } from 'pinia'




export const useCounterStore = defineStore('counter', () => {

 
 // //添加城市

const addcitys = ref([])
function add(city,templ,itemadcode){
      // addcitys.value = city
      // temp.value = templ
     
      addcitys.value.push({city,templ,itemadcode})
      // console.log(addcitys.value)
      // console.log(templ)
       ElMessage({
        type:'success',
        message:'添加成功',
        showClose: true,
        duration: 4000
    })
     console.log(1)
     console.log(addcitys.value)
}

// 删除城市
function handleDelete(index) {
    addcitys.value.splice(index, 1)
}

// 数据本地化存储
watch(
  addcitys,
  ()=>{
    localStorage.setItem('addcitys',JSON.stringify(addcitys.value))
  },
  {
    deep:true
  }
)

onMounted(() => {
  if (localStorage.getItem('addcitys')) {
    addcitys.value = JSON.parse(localStorage.getItem('addcitys'))
    console.log(addcitys.value)
  }
})

  return { addcitys,add,handleDelete}
})
